Ext.ns("sh.mytool.cv.module");

sh.mytool.cv.module.ColumnPanel = Ext.extend(Ext.tree.TreePanel, {
    // enableDD: true,
    power : "",
    containerScroll : true,
    border : false,
    autoScroll : true,
    rootUrl : "",
    loaderUrl : "",
    getUrl : "",
    editUrl : "",
    removeUrl : "",
    rootId : 0,
    initComponent : function() {
        Ext.Ajax.request({
            url : this.rootUrl,
            params : {
                id : this.rootId
            },
            method : "post",
            async : false, // ext-basex.js
            scope : this,
            callback : function(options, success, response) {
                var result = Ext.decode(response.responseText);
                result.leaf = false;
                this.root = new Ext.tree.AsyncTreeNode(result);
            }
        });
        this.loader = new Ext.tree.TreeLoader({
            url : this.loaderUrl,
            baseParams : {
                parentId : null
            },
            // preloadChildren: true,
            listeners : {
                beforeload : function(loader, node) {
                    loader.baseParams.parentId = node.id;
                }
            }
        });
        this.contextMenu = new Ext.menu.Menu({
            items : [ {
                id : 'add-column',
                text : 'Add',
                scope : this,
                hidden: this.power.hidden,
                handler : this.toAdd
            }, {
                id : 'modify-column',
                text : 'Modify',
                scope : this,
                hidden: this.power.hidden,
                handler : this.toModify
            }, {
                id : 'remove-column',
                text : 'Remove',
                scope : this,
                hidden: this.power.hidden,
                handler : this.toRemove
            }, {
                id : 'moveup-column',
                text : 'MoveUp',
                scope : this,
                hidden: this.power.hidden,
                handler : this.toMoveUp
            }, {
                id : 'movedown-column',
                text : 'MoveDown',
                scope : this,
                hidden: this.power.hidden,
                handler : this.toMoveDown
            } ]
        });
        this.listeners = {
            contextmenu : function(node, e) {
                node.select();
                var c = node.getOwnerTree().contextMenu;
                // c.contextNode = node; // menuItem.parentMenu.contextNode;
                c.showAt(e.getXY());
            }
        };
        sh.mytool.cv.module.ColumnPanel.superclass.initComponent.call(this); // bindbehind?
        this.root.expand(true); // expand all
    },
    getSelectNode: function(){
        return this.getSelectionModel().getSelectedNode();
    },
    // T node operation is better
    toMoveUp: function(){
        var node = this.getSelectNode();
        if(!node.isFirst()){
//            var targetNode = node.previousSibling;
//            targetNode.nextSibling = node;
            this.toMove(node.previousSibling, node);
        }
    },
    toMove: function(changeNode, node){
        var wartMsg = Ext.Msg.wait("", "Please Wait...");
        Ext.Ajax.request({
            url : this.moveUrl,
            params : {
                id : node.id,
                changeId: changeNode.id
            },
            method : "post",
            async : false,
            scope : this,
            success : function(resp, opts) {
                wartMsg.hide();
                Ext.Msg.alert("Tip", "Success!");
                this.toRefresh();
            },
            failure : function(resp, opts) {
                wartMsg.hide();
                Ext.Msg.alert("Tip", "Failed!");
            }
        });
    },
    toMoveDown: function(){
        var node = this.getSelectNode();
        if(!node.isLast()){
            this.toMove(node.nextSibling, node);
        }
    },
    getEditWin : function() {
        if (!this.win) {
            var formPanel = new Ext.form.FormPanel({
                labelAlign : "right",
                labelWidth : 50,
                border : false,
                layout : "form",
                method : "post",
                items : [ {
                    height : 15,
                    border : false
                }, {
                    xtype : "hidden",
                    name : "id"
                }, {
                    xtype : "hidden",
                    name : "parentId"
                }, {
                    xtype : "textfield",
                    fieldLabel : "text",
                    name : "text"
                } ],
                buttonAlign : "center",
                buttons : [ {
                    text : "Save",
                    scope : this,
                    handler : this.submitForm
                },
                // {text: "Reset", scope: this, handler: this.resetForm},
                {
                    text : "Close",
                    scope : this,
                    handler : function() {
                        this.win.hide();
                    }
                } ]
            });
            this.win = new Ext.Window({
                title : "Edit Column",
                width : 350,
                height : 250,
                closeAction : 'hide',
                modal : true,
                layout : 'fit',
                items : [ formPanel ]
            });
        }
    },
    toAdd : function() {
        this.getEditWin();
        this.cleanForm();
        // setHidden
        var selectedNode = this.getSelectNode();
        this.win.getComponent(0).getForm().setValues({
            parentId : selectedNode.id
        });
        this.win.show();
    },
    toModify : function() {
        this.getEditWin();
        this.loadForm();
        this.win.show();
    },
    toRemove : function() {
        Ext.Msg.confirm("Tip", "Delete Column ?", function(btn) {
            if (btn == "yes") {
                var id = this.getSelectNode().id;
                var wartMsg = Ext.Msg.wait("", "Please Wait...");
                Ext.Ajax.request({
                    url : this.removeUrl,
                    params : {
                        id : id
                    },
                    method : "post",
                    async : false,
                    scope : this,
                    success : function(resp, opts) {
                        wartMsg.hide();
                        Ext.Msg.alert("Tip", "Success!");
                        this.toRefresh();
                    },
                    failure : function(resp, opts) {
                        wartMsg.hide();
                        Ext.Msg.alert("Tip", "Failed!");
                    }
                });
            }
        }, this);
    },
    // T node operation is better
    toRefresh : function() {
        var node = this.getSelectNode();
        var parentNode = node.parentNode;
        // root
        if (!parentNode) {
            parentNode = node;
        }
        parentNode.reload();
        parentNode.expand(true);
    },
    cleanForm : function() {
        // this.formData = {};
        // this.win.getComponent(0).getForm().setValues(this.formData);
        this.win.getComponent(0).getForm().reset();
    },
    loadForm : function() {
        var id = this.getSelectNode().id;
        this.win.getComponent(0).getForm().load({
            url : this.getUrl,
            params : {
                id : id
            },
            method : "post",
            waitMsg : "",
            success : function(form, action) {
                // form.setValues(action.result.data);
            },
            failure : function(form, acition) {
            }
        });
    },
    // T
    resetForm : function() {
        this.win.getComponent(0).getForm().reset();
        // var form = this.win.getComponent(0).getForm();
        // var key = form.findField("id").getValue();
        // form.reset();
        // form.setValues({
        // id: key
        // });
    },
    submitForm : function() {
        this.win.getComponent(0).getForm().submit({
            url : this.editUrl,
            scope : this,
            waitMsg : "",
            success : function(form, action) {
                this.win.hide();
                Ext.Msg.alert("Tip", "Success!");
                this.toRefresh();
            },
            failure : function(form, acition) {
                Ext.Msg.alert("Tip", "Failed!");
            }
        });
    }
});
